package com.a16.yuejian.yuejian.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.activity.ClubActivity;
import com.a16.yuejian.yuejian.javabean.Club;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 王子恒 on 2017/9/30.
 */

public class ClubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    public ClubAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.club_cardview,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Club entity = (Club) mDataList.get(position);
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.club_name.setText(entity.getClubname());
        Picasso.with(mContext).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+entity.getClubpic()).into(viewHolder.club_img);
        viewHolder.club_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sp = mContext.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                editor = sp.edit();
                editor.putString("clubid", entity.getClubid());
                editor.commit();
                Intent intent = new Intent(mContext,ClubActivity.class);
                intent.putExtra("clubname",entity.getClubname());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView club_img;
        TextView club_name;
        LinearLayout club_container;

        public ViewHolder(View itemView) {
            super(itemView);
            club_name = (TextView) itemView.findViewById(R.id.club_name);
            club_img = (ImageView) itemView.findViewById(R.id.club_img);
            club_container = (LinearLayout) itemView.findViewById(R.id.club_container);
        }
    }
}
